Introduction to XML in Flash
by senocular
Preloaders With XML
Just like anything else loaded into Flash, you
can also create preloaders for XML. Generally,
however, since XML is usually light on the loading
side, preloaders aren't needed. A general "Now
Loading" message usually suffices. But
sometimes, for those real hefty files, you may
want a preloader to show the status of the XML
loading.
If you're worried about learning a whole new
way of making preloaders for XML, don't be.
Making a preloader for XML is exactly like making
one for a loaded SWF or JPEG. The only difference
is that instead of calling getBytesLoaded and
getBytesTotal from a movie clip, you're calling
it from your XML instance. So really, you can
use preloaders created for movie clips with
XML so long as you reference the XML object
to get bytes loaded and bytes total. Here's
a quick example of a preloader that can work
with XML as well as movie clips:
- preloadbar_mc.onEnterFrame = function(){
- if (!this.target) return (0);
- var loaded = target.getBytesLoaded();
- var total = target.getBytesTotal();
-
- var scale = 0;
- if (loaded && total){
- var percent = loaded/total;
- scale = 100 * percent;
- }
-
- this._xscale = scale;
- }
-
- preloadbar_mc.target = my_xml;
Where preloadbar_mc is a horizonally scaling
movie clip representing the preloader and my_xml
is the XML instance you wish to show the preloader
for. The onEnterFrame event of the preloadbar_mc
runs the preloader using a generic target to
get bytes loaded and bytes total. Whether this
is a movie clip or an XML instance, as long
as getBytesLoaded and getBytesTotal work, it
doesn't matter.
|